package com.augurit.gzcgwsp.sendNotice.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import org.apache.commons.lang.StringUtils;
import org.dozer.DozerBeanMapper;
import org.hibernate.SQLQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import MQAPI.AceeptOB;
import MQAPI.BlockOB;
import MQAPI.CompleteOB;
import MQAPI.MQAPIOB;
import MQAPI.ResumeOB;
import MQAPI.SubmitOB;
import MQAPI.SupplyAcceptOB;
import MQAPI.SupplyOB;
import MQAPI.WSSB;
import MQAPI.WorkRuslInfoOB;

import com.augurit.common.base.extmodel.BasicCombo;
import com.augurit.common.syslog.tool.SysTraceLogWriter;
import com.augurit.common.util.PageUtils;
import com.augurit.gzcgwsp.commonex.util.lang.DateUtilsEX;
import com.augurit.gzcgwsp.sendNotice.convert.YwZhengwuInfoConvert;
import com.augurit.gzcgwsp.sendNotice.dao.YwZhengwuInfoDao;
import com.augurit.gzcgwsp.sendNotice.entity.YwZhengwuInfo;
import com.augurit.gzcgwsp.sendNotice.service.IYwZhengwuInfoService;
import com.augurit.gzcgwsp.sendNotice.web.form.YwZhengwuInfoForm;
import com.augurit.wf.common.service.impl.WfBusClientServiceImpl;

@Service
@Transactional
public class YwZhengwuInfoServiceImpl extends WfBusClientServiceImpl<YwZhengwuInfoForm> implements IYwZhengwuInfoService{
	Logger logger = (Logger) LoggerFactory.getLogger(YwZhengwuInfoServiceImpl.class);
    static Locale locale = Locale.getDefault(); 
    static ResourceBundle localResource = ResourceBundle.getBundle("interfaceExchange", locale); 
	private static final String sourceNode = localResource.getString("SourceNode");//源节点
	private static final String destinationNode = localResource.getString("DestinationNode");//目标节点
	@Autowired
	private YwZhengwuInfoDao ywZhengwuInfoDao;
	

	public static MQAPIOB SendAPI = MQAPIOB.Create("/usr/DC_CLIENT");
	
	/**
	 * 保存webservice接口的信息
	 * @param ywZhengwuInfo
	 */
	public void save(YwZhengwuInfo ywZhengwuInfo){
		ywZhengwuInfoDao.save(ywZhengwuInfo);
	}
	
	private void saveEntity(YwZhengwuInfo ywZhengwuInfo,String sphjdmmc){
		String sql=" from YwZhengwuInfo where serialNo='"+ywZhengwuInfo.getSerialNo()+"' and sphjdmmc='"+sphjdmmc+"'";
		String sqlsl=" from YwZhengwuInfo where serialNo='"+ywZhengwuInfo.getSerialNo()+"' and sphjdmmc='受理'";
		List<YwZhengwuInfo> list=this.ywZhengwuInfoDao.find(sql);
		List<YwZhengwuInfo> list1=this.ywZhengwuInfoDao.find(sqlsl);
		if(list.size()<1 && list1.size()>0){
			ywZhengwuInfoDao.save(ywZhengwuInfo);
		}
	}
	
	/**
	 * 根据查询编号和审批环节代码查询YwZhengwuInfo
	 * @param serialNo 查询编号
	 * @param sphjdm 审批环节代码
	 * @return
	 */
	public YwZhengwuInfo getYwZhengwuInfoBySerialNoAndSphjdm(String serialNo,String sphjdm){
		String sql=" from YwZhengwuInfo where serialNo='"+serialNo+"' and sphjdm='"+sphjdm+"'";
		List<YwZhengwuInfo> list=this.ywZhengwuInfoDao.find(sql);
		if(list.size()>0){
			return (YwZhengwuInfo)(list.get(0));
		}
		return null;
	}
	
	/**
	 * 获得还没有发送的记录 进行交换 政务中心接口
	 * @return
	 */
	public void exchangeYwZhengwuInfo(){
		try {
			Page<YwZhengwuInfo> ywZhengwuInfoPage = new Page<YwZhengwuInfo>(2000);
			Page<YwZhengwuInfo> ywZhengwuInfoPage2 = ywZhengwuInfoDao.getYwZhengwuInfo(ywZhengwuInfoPage);
			
			if(ywZhengwuInfoPage2.getTotalCount()>0){
				List<YwZhengwuInfo> ywZhengwuInfoList = ywZhengwuInfoPage2.getResult();
				logger.info("-数量-"+ywZhengwuInfoList.size());
				for(int i = 0;i<ywZhengwuInfoList.size();i++){
					YwZhengwuInfo ywZhengwuInfo = ywZhengwuInfoList.get(i);
					int flag = this.exchangeYwZhengwuInfo(ywZhengwuInfo);
					if(flag == 1){//交换成功
						ywZhengwuInfoDao.changState(ywZhengwuInfo.getId());//设置为 已发送
					}
				}
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
		
		//处理特别程序（延期、公示）结束  开始，如果特别程序结束时间在今天前面，就把IsSended字段设置为空
		try {
			Page<YwZhengwuInfo> ywZhengwuInfoPageForTbcx = new Page<YwZhengwuInfo>(2000);
			Page<YwZhengwuInfo> ywZhengwuInfoPageForTbcxEnd2 = ywZhengwuInfoDao.getYwZhengwuInfoForTbcxEnd(ywZhengwuInfoPageForTbcx);
			if(ywZhengwuInfoPageForTbcxEnd2.getTotalCount()>0){
				List<YwZhengwuInfo> ywZhengwuInfoList = ywZhengwuInfoPageForTbcxEnd2.getResult();
				for(int i = 0;i<ywZhengwuInfoList.size();i++){
					YwZhengwuInfo ywZhengwuInfo = ywZhengwuInfoList.get(i);
					ywZhengwuInfoDao.updateIsSended(ywZhengwuInfo.getId());//设置为 空				
				}
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
		//处理特别程序（延期、公示）结束  结束
		
	}
	
	/**
	 * 交换办结前，根据受理号查询是否有还没有结束延期的记录,有则先交换
	 * @param serialNo
	 */
	public void doYqEnd(String serialNo){
		int yqEndValue = 0;
		YwZhengwuInfo ywZhengwuInfo = ywZhengwuInfoDao.doYqEnd(serialNo);
		if(ywZhengwuInfo != null){
			ResumeOB resumeOB = new ResumeOB();
			resumeOB.setSourceNode(ywZhengwuInfo.getSourceNode());
			resumeOB.setDestinationNode(ywZhengwuInfo.getDestinationNode());
			resumeOB.setSerialNO(ywZhengwuInfo.getSerialNo());
			resumeOB.setPassword(ywZhengwuInfo.getPassword());
			resumeOB.setSJBBH(Integer.parseInt(ywZhengwuInfo.getSjbbh()));//数据的版本号，默认为1，如果报送数据后数据发生改变需要重报的，以2、3、4…每次加1的方式填写版本号，务必谨慎使用
			resumeOB.setXH(Integer.parseInt(ywZhengwuInfo.getXh()));//特别程序申请的序号，第一个特别程序申请的序号1，后面的序号以2、3、4…每次加1 的方式报序
			resumeOB.setSXBM(ywZhengwuInfo.getSxbm());//委办局业务系统事项编号
			resumeOB.setYXTYWLSH(ywZhengwuInfo.getYxtywlsh());//原系统业务流水号
			resumeOB.setTBCXJG(ywZhengwuInfo.getTbcxjg());//特别程序结果
			resumeOB.setJGCSRQ(ywZhengwuInfo.getJgcsrq());//结果产生日期
			resumeOB.setTBCXJSRQ(ywZhengwuInfo.getTbcxjsrq());//特别程序结束日期
			resumeOB.setTBCXSFJE(ywZhengwuInfo.getTbcxsfje());//特别程序收费金额
			yqEndValue = SendAPI.Resume(resumeOB);
			ywZhengwuInfoDao.changState(ywZhengwuInfo.getId());//设置为 已发送
			
			logger.info("统一查询号 == "+ywZhengwuInfo.getSerialNo());
			logger.info("查询密码 == "+ywZhengwuInfo.getPassword());
			logger.info("环节代码 == "+ywZhengwuInfo.getSphjdm());
			logger.info("环节名称 == "+ywZhengwuInfo.getSphjdmmc());
			logger.info("政务接口返回参数(1：发送成功 2：发送异常（检查环境配置文件及网络连接情况）3:传入的参数值为空) yqEndValue == "+yqEndValue);
		
		}
	}
	
	/**
	 * 当webservice调用成功，则把改记录的isSended字段的值改成1
	 * @param id
	 */
	public void changState(long id){
		ywZhengwuInfoDao.changState(id);
	}
	
	/**
	 * 调用政务中心接口，进行数据交换
	 * @param ywZhengwuInfo
	 */
	public int exchangeYwZhengwuInfo(YwZhengwuInfo ywZhengwuInfo){
		
		int intValue= 0;
		try{
			if("000".equals(ywZhengwuInfo.getSphjdm())){//网上申报
				WSSB wssb = new WSSB();
				wssb.setSourceNode(ywZhengwuInfo.getSourceNode());//交换源节点
				wssb.setDestinationNode(ywZhengwuInfo.getDestinationNode());//交换目标节点
				wssb.setSerialNO(ywZhengwuInfo.getSerialNo());//统一查询号
				wssb.setPassword(ywZhengwuInfo.getPassword());//查询密码
				wssb.setSJBBH(Integer.parseInt(ywZhengwuInfo.getSjbbh()));//
				wssb.setSXBM(ywZhengwuInfo.getSxbm());//委局业务系统事项编号
				wssb.setYXTYWLSH(ywZhengwuInfo.getYxtywlsh());//原系统业务流水号
				wssb.setSPSXMC(ywZhengwuInfo.getSpsxmc());//审批事项名称
				wssb.setSQRHSQDW(ywZhengwuInfo.getSqrhsqdw());//申请人或申请单位
				wssb.setSQRXM(ywZhengwuInfo.getSqrxm());//申请人姓名
				wssb.setSQRSJ(ywZhengwuInfo.getSqrsj());//申请人手机
				wssb.setSQRDH(ywZhengwuInfo.getSqrdh());//申请人电话
				wssb.setWSTJSJ(ywZhengwuInfo.getWstjsj());//网上提交时间
				wssb.setWSTXCLQD(ywZhengwuInfo.getWstxclqd());//网上填写材料清单
				wssb.setHZBH(ywZhengwuInfo.getHzbh());//回执编号
				
				String strTemp=SendAPI.getMaxNumber();
				wssb.setBYZDA(strTemp);
			    intValue= SendAPI.OnlineAccept(wssb);
			    wssb=null;
			}else if("001".equals(ywZhengwuInfo.getSphjdm())){//受理                       FLGDGXD 和 FSYWGXD两个不可空的字段，还没有搞
				AceeptOB slAceept = new AceeptOB();
				slAceept.setSourceNode(ywZhengwuInfo.getSourceNode());    			
			    slAceept.setDestinationNode(ywZhengwuInfo.getDestinationNode());
			    slAceept.setSJBBH(Integer.parseInt(ywZhengwuInfo.getSjbbh()));//数据版本号		   
			    slAceept.setGDBLSX(ywZhengwuInfo.getGdblsx());//按法律、法规规定的办理此项审批事项的时限
			    slAceept.setGDBLSXDW(ywZhengwuInfo.getGdblsxdw());//规定办理时限的单位  G – 工作日,  Z – 自然日
			    slAceept.setGDSF(ywZhengwuInfo.getGdsf());//按法律、法规规定的收费情况。
	//		    slAceept.setXMMC("");  //申请审批的项目的具体名称。
			    slAceept.setSLJGZZJGDM(ywZhengwuInfo.getSljgzzjgdm());//受理机关组织机构代码
		        slAceept.setSLJGMC(ywZhengwuInfo.getSljgmc());//受理机关名称
		        slAceept.setSLDWCBRXM(ywZhengwuInfo.getSldwcbrxm());//受理单位办理人姓名      注：因为这个字段只有
				slAceept.setFLGDGXD(ywZhengwuInfo.getFlgdgxd());
				slAceept.setFSYWGXD(ywZhengwuInfo.getFsywgxd());
	//	        slAceept.setSLDWCBRGH("");//受理单位办理人工号 可空
	//	        slAceept.setSLJTDD("");//受理具体地点  可空
		        slAceept.setSLSJ(ywZhengwuInfo.getSlsj());//受理时间
		        slAceept.setSLZLQD(ywZhengwuInfo.getSlzlqd());//受理资料清单
		        slAceept.setSLYF(ywZhengwuInfo.getSlyf());//受理与否   0 – 受理,1 – 不受理
		        slAceept.setTJFS(ywZhengwuInfo.getTjfs());//提交方式 0 – 窗口提交,1 – 网上提交,2 – 信函,3 – 电报,4 – 电传,5 – 传真,6 – 邮件,7 – 电子数据交换,8 – 其它
		        
		        slAceept.setSerialNO(ywZhengwuInfo.getSerialNo()); 
		        slAceept.setPassword(ywZhengwuInfo.getPassword());
		        slAceept.setSQDWHSQRXM(ywZhengwuInfo.getSqdwhsqrxm());//申请单位或申请人名称
			    slAceept.setSQDWJBRXM(ywZhengwuInfo.getSqdwjbrxm());//申请单位经办人姓名  
			    slAceept.setHZBH(ywZhengwuInfo.getHzbh());//回执编号   此受理业务回执的编号，按《行政许可法》规定必须要有回执。可以是统一查询号。
			    slAceept.setYXTYWLSH(ywZhengwuInfo.getYxtywlsh());//原系统业务流水号
			    slAceept.setSQDWLXDH(ywZhengwuInfo.getSqdwlxdh());//申请单位联系电话 可空
			    slAceept.setSPSXMC(ywZhengwuInfo.getSpsxmc());//审批事项名称
			    slAceept.setXMMC(ywZhengwuInfo.getXmmc());  //申请审批的项目的具体名称。
			    slAceept.setSXBM(ywZhengwuInfo.getSxbm());//委局业务系统事项编号
			    
			    String strTemp=SendAPI.getMaxNumber();
			    slAceept.setBYZDA(strTemp);
			    intValue= SendAPI.Accept(slAceept);
	//		    intValue= new MQAPIClass().Accept(slAceept);
	        	slAceept=null;
			}else if("002".equals(ywZhengwuInfo.getSphjdm()) || "003".equals(ywZhengwuInfo.getSphjdm()) || "004".equals(ywZhengwuInfo.getSphjdm())){//承办、审核、审批
				SubmitOB sl = new SubmitOB();
				sl.setDestinationNode(ywZhengwuInfo.getDestinationNode());
				sl.setSourceNode(ywZhengwuInfo.getSourceNode());
				sl.setSJBBH(Integer.parseInt(ywZhengwuInfo.getSjbbh()));//数据版本号  默认为1
				sl.setSPBZH(Integer.parseInt(ywZhengwuInfo.getSpbzh()));//审批步骤号   审批的步骤号，第一个审批的步骤号为1，后面的步骤号以2、3、4…每次加1 的方式
				sl.setSPHJDM(ywZhengwuInfo.getSphjdm());//审批环节代码
				sl.setSPHJMC(ywZhengwuInfo.getSphjdmmc());//审批环节名称   审批环节代码对应的名称
				sl.setSPRXM(ywZhengwuInfo.getSprxm());//审批人姓名  
				sl.setSPRZWDM(ywZhengwuInfo.getSprzwdm());//审批人职务代码
				sl.setSPRZWMC(ywZhengwuInfo.getSprzwmc());//审批人职务名称     还没有值
		          
				sl.setSPYJ(ywZhengwuInfo.getSpyj());//审批意见
				sl.setSPSJ(ywZhengwuInfo.getSpsj());//审批时间  YYYY-MM-DD hh24:mi:ss
				sl.setSerialNO(ywZhengwuInfo.getSerialNo()); 
				sl.setPassword(ywZhengwuInfo.getPassword());
				sl.setYXTYWLSH(ywZhengwuInfo.getYxtywlsh());//原系统业务流水号
				sl.setSXBM(ywZhengwuInfo.getSxbm());//委局业务系统事项编号
				
				String strTemp=SendAPI.getMaxNumber();
				sl.setBYZDA(strTemp);
				
			    intValue= SendAPI.Submit(sl);
//				intValue= new MQAPIClass().Submit(sl);
	        	sl=null;
			}else if("005".equals(ywZhengwuInfo.getSphjdm())){//办结
				doYqEnd(ywZhengwuInfo.getSerialNo());
				
				CompleteOB s4 = new CompleteOB();
				s4.setSourceNode(ywZhengwuInfo.getSourceNode());
				s4.setDestinationNode(ywZhengwuInfo.getDestinationNode());
				s4.setSJBBH(Integer.parseInt(ywZhengwuInfo.getSjbbh()));//数据版本号
				s4.setBJJGZZJGDM(ywZhengwuInfo.getBjjgzzjgdm());//办结机关组织机构代码
				s4.setBJJGMC(ywZhengwuInfo.getBjjgmc());//办结机关名称
				s4.setBJSJ(ywZhengwuInfo.getBjsj());//办结时间  YYYY-MM-DD hh24:mi:ss
				s4.setBJJG(ywZhengwuInfo.getBjjg());//办结结果  0 – 出证办结（正常产生证照、批文的办结）,1 – 退回办结（退回或驳回申请的办结）,2 – 作废办结（指业务处理上无效的纪录）,3 – 删除办结（指录入错误、操作错误等技术上的无效纪录）,4 – 转报办结（指转报其他单位或上级单位的办结情况）,5 – 补交不来办结（指出现补交告知时，通知之后，申请人长期不来补交材料的办结,6 – 办结（除以上所述情况外的办结
				s4.setBLJGMS(ywZhengwuInfo.getBljgms());//办理结果描述
				s4.setSFJE(Float.parseFloat(ywZhengwuInfo.getSfje()));//收费金额  收费的金额，保留小数点后2位。不收费填0
				s4.setJEDWDM(ywZhengwuInfo.getJedwdm());//金额单位代码  金额单位的代码。详细列表参见附件《国际标准货币代码表》。
				
				s4.setSerialNO(ywZhengwuInfo.getSerialNo()); 
				s4.setPassword(ywZhengwuInfo.getPassword());
				s4.setYXTYWLSH(ywZhengwuInfo.getYxtywlsh());//原系统业务流水号
			    s4.setSXBM(ywZhengwuInfo.getSxbm());//委局业务系统事项编号
			    
			    String strTemp=SendAPI.getMaxNumber();
				s4.setBYZDA(strTemp);
				
			    intValue= SendAPI.Complete(s4);
			    
//			    intValue= new MQAPIClass().Complete(s4);
	        	s4=null;
			}else if("006".equals(ywZhengwuInfo.getSphjdm())){//办事结果表单
				WorkRuslInfoOB wrio = new WorkRuslInfoOB();
				
				wrio.setSourceNode(ywZhengwuInfo.getSourceNode());//交换源节点编号
				wrio.setDestinationNode(ywZhengwuInfo.getDestinationNode());//交换的目的节点
				wrio.setSerialNO(ywZhengwuInfo.getSerialNo());//统一查询号
				wrio.setYXTYWLSH(ywZhengwuInfo.getYxtywlsh());//原系统业务流水号
				wrio.setZJHPWBH(StringUtils.isEmpty(ywZhengwuInfo.getZjhpwbh())?"空":ywZhengwuInfo.getZjhpwbh());//证件/批文的编号
				wrio.setZJHPWMC(StringUtils.isEmpty(ywZhengwuInfo.getZjhpwmc())?"空":ywZhengwuInfo.getZjhpwmc());//证件/批文名称
				wrio.setSQQYMC(ywZhengwuInfo.getSqqymc());//申请企业名称
				wrio.setSQQYDZ(ywZhengwuInfo.getSqqydz());//申请企业地址
				wrio.setFFJGZZJGDM(ywZhengwuInfo.getBjjgzzjgdm());//发放结果机关组织机构代码
				wrio.setFFJGJGMC(ywZhengwuInfo.getBjjgmc());//发放结果机关名称
				wrio.setFFSJ(ywZhengwuInfo.getFfsj()==null?new Date():ywZhengwuInfo.getFfsj());//发放时间
				wrio.setFFJGLX(Integer.parseInt(ywZhengwuInfo.getFfjglx()));//发放结果的类型
				
				String strTemp=SendAPI.getMaxNumber();
				wrio.setBYZDA(strTemp);
				
				intValue = SendAPI.WorkRuslInfo(wrio);
			}else if("007".equals(ywZhengwuInfo.getSphjdm()) || "011".equals(ywZhengwuInfo.getSphjdm())){//特别程序开始
				BlockOB blockOB = new BlockOB();
				blockOB.setSourceNode(ywZhengwuInfo.getSourceNode());//交换源节点编号
				blockOB.setDestinationNode(ywZhengwuInfo.getDestinationNode());//交换的目的节点
				blockOB.setSerialNO(ywZhengwuInfo.getSerialNo());//统一查询号
				blockOB.setPassword(ywZhengwuInfo.getPassword());//统一查询密码
				blockOB.setSJBBH(Integer.parseInt(ywZhengwuInfo.getSjbbh()));//数据的版本号，默认为1，如果报送数据后数据发生改变需要重报的，以2、3、4…每次加1的方式填写版本号，务必谨慎使用
				blockOB.setXH(Integer.parseInt(ywZhengwuInfo.getXh()));//特别程序申请的序号，第一个特别程序申请的序号1，后面的序号以2、3、4…每次加1 的方式报序
				blockOB.setSXBM(ywZhengwuInfo.getSxbm());//委办局业务系统事项编号
				blockOB.setYXTYWLSH(ywZhengwuInfo.getYxtywlsh());//原系统业务流水号
				blockOB.setTBCXZL(ywZhengwuInfo.getTbcxzl());//特别程序的种类： A – 延长审批
				blockOB.setTBCXZLMC(ywZhengwuInfo.getTbcxzlmc());//特别程序种类名称
				blockOB.setTBCXKSRQ(ywZhengwuInfo.getTbcxksrq());//特别程序开始日期
				blockOB.setTBCXPZR(ywZhengwuInfo.getTbcxpzr());//特别程序批准人
				blockOB.setTBCXQDLYHYJ(ywZhengwuInfo.getTbcxqdlyhyj());//特别程序启动理由或依据
				blockOB.setSQNR(ywZhengwuInfo.getSqnr());//申请内容
				blockOB.setTBCXSX(Integer.parseInt(ywZhengwuInfo.getTbcxsx()));//特别程序时限
				blockOB.setTBCXSXDW("G");//特别程序时限单位  没有保存到数据库，在这里加上
				
				String strTemp=SendAPI.getMaxNumber();
				blockOB.setBYZDA(strTemp);
				
				intValue = SendAPI.Block(blockOB);
			}else if("008".equals(ywZhengwuInfo.getSphjdm()) || "012".equals(ywZhengwuInfo.getSphjdm())){//特别程序结束
				ResumeOB resumeOB = new ResumeOB();
				resumeOB.setSourceNode(ywZhengwuInfo.getSourceNode());
				resumeOB.setDestinationNode(ywZhengwuInfo.getDestinationNode());
				resumeOB.setSerialNO(ywZhengwuInfo.getSerialNo());
				resumeOB.setPassword(ywZhengwuInfo.getPassword());
				resumeOB.setSJBBH(Integer.parseInt(ywZhengwuInfo.getSjbbh()));//数据的版本号，默认为1，如果报送数据后数据发生改变需要重报的，以2、3、4…每次加1的方式填写版本号，务必谨慎使用
				resumeOB.setXH(Integer.parseInt(ywZhengwuInfo.getXh()));//特别程序申请的序号，第一个特别程序申请的序号1，后面的序号以2、3、4…每次加1 的方式报序
				resumeOB.setSXBM(ywZhengwuInfo.getSxbm());//委办局业务系统事项编号
				resumeOB.setYXTYWLSH(ywZhengwuInfo.getYxtywlsh());//原系统业务流水号
				resumeOB.setTBCXJG(ywZhengwuInfo.getTbcxjg());//特别程序结果
				resumeOB.setJGCSRQ(ywZhengwuInfo.getJgcsrq());//结果产生日期
				resumeOB.setTBCXJSRQ(ywZhengwuInfo.getTbcxjsrq());//特别程序结束日期
				resumeOB.setTBCXSFJE(ywZhengwuInfo.getTbcxsfje());//特别程序收费金额
				
				String strTemp=SendAPI.getMaxNumber();
				resumeOB.setBYZDA(strTemp);
				
				intValue = SendAPI.Resume(resumeOB);
			}else if("009".equals(ywZhengwuInfo.getSphjdm())){//补交告知
				SupplyOB supplyOB = new SupplyOB();
				supplyOB.setSourceNode(ywZhengwuInfo.getSourceNode());
				supplyOB.setDestinationNode(ywZhengwuInfo.getDestinationNode());
				supplyOB.setSerialNO(ywZhengwuInfo.getSerialNo());
				supplyOB.setPassword(ywZhengwuInfo.getPassword());
				supplyOB.setSJBBH(Integer.parseInt(ywZhengwuInfo.getSjbbh()));//数据的版本号，默认为1，如果报送数据后数据发生改变需要重报的，以2、3、4…每次加1的方式填写版本号，务必谨慎使用
				supplyOB.setSXBM(ywZhengwuInfo.getSxbm());//委办局业务系统事项编号
				supplyOB.setYXTYWLSH(ywZhengwuInfo.getYxtywlsh());//原系统业务流水号
				supplyOB.setBJGZFCR(ywZhengwuInfo.getBjgzfcr());//补交告知发出人
				supplyOB.setBJGZYY(ywZhengwuInfo.getBjgzyy());//补交告知原因
				supplyOB.setBJCLGZQD(ywZhengwuInfo.getBjclgzqd());//补交材料告知清单
				supplyOB.setBJGZSJ(ywZhengwuInfo.getBjgzsj());//补交告知时间
				
				String strTemp=SendAPI.getMaxNumber();
				supplyOB.setBYZDA(strTemp);
				
				intValue = SendAPI.Supply(supplyOB);
			}else if("010".equals(ywZhengwuInfo.getSphjdm())){//补交受理
				SupplyAcceptOB supplyAcceptOB = new SupplyAcceptOB();
				supplyAcceptOB.setSourceNode(ywZhengwuInfo.getSourceNode());
				supplyAcceptOB.setDestinationNode(ywZhengwuInfo.getDestinationNode());
				supplyAcceptOB.setSerialNO(ywZhengwuInfo.getSerialNo());
				supplyAcceptOB.setPassword(ywZhengwuInfo.getPassword());
				supplyAcceptOB.setSJBBH(Integer.parseInt(ywZhengwuInfo.getSjbbh()));//数据的版本号，默认为1，如果报送数据后数据发生改变需要重报的，以2、3、4…每次加1的方式填写版本号，务必谨慎使用
				supplyAcceptOB.setSXBM(ywZhengwuInfo.getSxbm());//委办局业务系统事项编号
				supplyAcceptOB.setYXTYWLSH(ywZhengwuInfo.getYxtywlsh());//原系统业务流水号
				supplyAcceptOB.setSLDWCBRXM(ywZhengwuInfo.getSldwcbrxm());//补交受理单位办理人姓名
				supplyAcceptOB.setBJSLJTDD(ywZhengwuInfo.getBjsljtdd());//补交受理具体地点
				supplyAcceptOB.setBJSJ(ywZhengwuInfo.getBjsj());//补交时间
				supplyAcceptOB.setBJCLQD(ywZhengwuInfo.getBjclqd());//补交材料清单
				
				String strTemp=SendAPI.getMaxNumber();
				supplyAcceptOB.setBYZDA(strTemp);
				
				intValue = SendAPI.SupplyAccept(supplyAcceptOB);
			}
		}catch(Exception e){
			logger.error("(接口)政务交换接口异常"+SysTraceLogWriter.MESSAGE_SEPARATOR + e.getMessage(), e);
			e.printStackTrace();
			return 4;
		}
		logger.info("统一查询号 == "+ywZhengwuInfo.getSerialNo());
		logger.info("查询密码 == "+ywZhengwuInfo.getPassword());
		logger.info("环节代码 == "+ywZhengwuInfo.getSphjdm());
		logger.info("环节名称 == "+ywZhengwuInfo.getSphjdmmc());
		logger.info("政务接口返回参数(1：发送成功 2：发送异常（检查环境配置文件及网络连接情况）3:传入的参数值为空) == "+intValue);
		return intValue;
	}

	/*
	 * 保存补交受理
	 * @see com.augurit.gzcgwsp.sendNotice.service.IYwZhengwuInfoService#saveBjsl(java.util.Map)
	 */
	@Override
	public boolean saveBjsl(Map map) throws Exception{
		//受理编号
		String serialNo=(String) map.get("serialNo");
		String password=(String) map.get("password");
		String formId=(String) map.get("formId");
		String username=(String) map.get("username");
		String bjclqd=(String) map.get("bjclqd");
		String bjslsj=(String)map.get("bjslsj");//补交受理时间
		//先查询是否插入了补交告知
		String sql=" from YwZhengwuInfo where sphjdmmc='补交告知' and serialNo='"+serialNo+"'";
		List<YwZhengwuInfo> list=ywZhengwuInfoDao.find(sql);
		if(list.size()>0){
			YwZhengwuInfo info=list.get(0);
			if(!"1".equals(info.getIsSended())){
				logger.error(serialNo+"出现异常情况：补交材料还没发送");
			}
			//如果发送了补交材料
			Locale locale = Locale.getDefault(); 
			//ResourceBundle localResource = ResourceBundle.getBundle("interfaceExchange", locale); 
			String sxbm =(String)map.get("sxbm");	
			YwZhengwuInfo ywZhengwuInfo = new YwZhengwuInfo();
			ywZhengwuInfo.setSourceNode(sourceNode);
			ywZhengwuInfo.setDestinationNode(destinationNode);
			ywZhengwuInfo.setSerialNo(serialNo);
			ywZhengwuInfo.setPassword(password);
			ywZhengwuInfo.setSjbbh("1");//数据的版本号，默认为1，如果报送数据后数据发生改变需要重报的，以2、3、4…每次加1的方式填写版本号，务必谨慎使用
			ywZhengwuInfo.setSxbm(sxbm);//委办局业务系统事项编号
			ywZhengwuInfo.setYxtywlsh(formId);//原系统业务流水号
			ywZhengwuInfo.setSldwcbrxm(username);//补交受理单位办理人姓名
			ywZhengwuInfo.setBjsljtdd("广州市城市管理委员会");//补交受理具体地点
			if(StringUtils.isNotBlank(bjslsj)){
	    		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
	    		Date bjslDate = sdf.parse(bjslsj);
	    		ywZhengwuInfo.setBjsj(bjslDate);//补交时间
			}else{
				ywZhengwuInfo.setBjsj(new Date());//补交时间
			}
			
			ywZhengwuInfo.setBjclqd(bjclqd);//补交材料清单
			ywZhengwuInfo.setProduceDate(new Date());
			ywZhengwuInfo.setSphjdm("010");
//			ywZhengwuInfo.setIsSended("补交受理还没发送");
			ywZhengwuInfo.setSphjdmmc("补交受理");
			this.saveEntity(ywZhengwuInfo,"补交受理");
			return true;
		}
		
		return false;
	}

	/*
	 * 保存补交告知(non-Javadoc)
	 * @see com.augurit.gzcgwsp.sendNotice.service.IYwZhengwuInfoService#saveBjgz(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public void saveBjgz(String serialNo, String password, String sxbm,
			String ywId, String bjgzfcr, String bjgzyy, String bjclgzqd){
		    YwZhengwuInfo ywZhengwuInfo = new YwZhengwuInfo();
			ywZhengwuInfo.setSourceNode(sourceNode);
			ywZhengwuInfo.setDestinationNode(destinationNode);
			ywZhengwuInfo.setSerialNo(serialNo);
			ywZhengwuInfo.setPassword(password);
			ywZhengwuInfo.setSjbbh("1");//数据的版本号，默认为1，如果报送数据后数据发生改变需要重报的，以2、3、4…每次加1的方式填写版本号，务必谨慎使用
			ywZhengwuInfo.setSxbm(sxbm);//委办局业务系统事项编号
			ywZhengwuInfo.setYxtywlsh(ywId);//原系统业务流水号
			ywZhengwuInfo.setBjgzfcr(bjgzfcr);//补交告知发出人
			ywZhengwuInfo.setBjgzyy(bjgzyy);//补交告知原因
			ywZhengwuInfo.setBjclgzqd(bjclgzqd);//补交材料告知清单
			ywZhengwuInfo.setBjgzsj(new Date());//补交告知时间
			ywZhengwuInfo.setProduceDate(new Date());
			ywZhengwuInfo.setSphjdm("009");
//			ywZhengwuInfo.setIsSended("补交告知还没发送"); 
			ywZhengwuInfo.setSphjdmmc("补交告知");
			this.saveEntity(ywZhengwuInfo,"补交告知");
		
	}

	/**
	 * 
	 */
	@Override
	public Page<YwZhengwuInfoForm> searchPage(Page<YwZhengwuInfoForm> page,Map map) {
		StringBuffer hql = new StringBuffer();
		hql.append(" from YwZhengwuInfo t where 1=1 ");
		String serialNo = (String)map.get("serialNo");
		String isSended = (String)map.get("isSended");
		String produceDate = (String)map.get("produceDate");
		String xmmc = (String)map.get("xmmc");
		String sphjdmmc = (String)map.get("sphjdmmc");
		String sphjdm = (String)map.get("sphjdm");
		if(StringUtils.isNotBlank(sphjdm)){
			hql.append(" and sphjdm = '"+sphjdm+"'");
		}
		if(StringUtils.isNotBlank(serialNo)){
			hql.append(" and t.serialNo like '%"+serialNo+"%'");
		}
		if(StringUtils.isNotBlank(isSended)){
			if("发送失败".equals(isSended)){//失败
				hql.append(" and t.isSended is null");
			}else if("其他".equals(isSended)){//其他
				hql.append(" and t.isSended is not null and t.isSended != '1'");
			}else{//发送成功
				hql.append(" and t.isSended='"+isSended+"'");
			}			
		}
		if(StringUtils.isNotBlank(produceDate)){
			hql.append(" and to_char(t.produceDate,'yyyy-MM-dd') = '"+produceDate.substring(0, 10)+"'");
		}
		if(StringUtils.isNotBlank(xmmc)){
			hql.append(" and t.xmmc='"+xmmc+"'");
		}
		if(StringUtils.isNotBlank(sphjdmmc)){
			hql.append(" and t.sphjdmmc='"+sphjdmmc+"'");
		}
		hql.append(" order by id desc");
		Map paraMap=new HashMap();
		Page pg = PageUtils.newPageInstance(page);
		this.ywZhengwuInfoDao.findPage(pg, hql.toString(),paraMap);
		// 建立新的分页对象
		List<YwZhengwuInfoForm> list = this.convertForm(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}

	/**
	 * form转化
	 * @param result
	 * @return
	 */
	private List<YwZhengwuInfoForm> convertForm(List<YwZhengwuInfo> list) {
		if(list != null && list.size() > 0) {
			List<YwZhengwuInfoForm> result = new ArrayList();
			for(YwZhengwuInfo entity : list){
				YwZhengwuInfoForm form = new DozerBeanMapper().map(entity, YwZhengwuInfoForm.class);
				 if(form.getSprxm()==null){
					 form.setSprxm(form.getSldwcbrxm());
				 }
				 YwZhengwuInfo slInfo=this.getInfoByNo(form.getSerialNo(), "受理");
				 YwZhengwuInfo bjInfo=this.getInfoByNo(form.getSerialNo(), "办结");
				 
				 String sprName=form.getSprxm()==null?"":form.getSprxm();
				 form.setSprxm(sprName+"("+form.getSprzwmc()+")");
				 if("1".equals(form.getIsSended())){
					 form.setIsSended("发送成功");
				 }else if(StringUtils.isBlank(form.getIsSended())){
					 form.setIsSended("发送失败");
				 }else{
					 form.setIsSended("等待发送");
				 }
				 //构造受理时间+办结时间和
				//构造其他信息包括企业电话，联系人，补交告知原因，清单等信息
				 String sjrq="";
				 if(slInfo!=null){
					 sjrq=DateUtilsEX.dateFormate(slInfo.getSlsj(), "yyyy-MM-dd HH:MM:ss");
					 form.setSqdwhsqrxm(slInfo.getSqdwhsqrxm());
					 form.setXmmc(slInfo.getXmmc());
					 form.setOtherInfo("企业联系人:"+slInfo.getSqdwjbrxm()+
							 "，电话:"+slInfo.getSqdwlxdh()+"，受理资料清单："+
							 slInfo.getSlzlqd());
				 }
				if(bjInfo!=null){
					 String bjrq=DateUtilsEX.dateFormate(bjInfo.getBjsj(), "yyyy-MM-dd HH:MM:ss");
					  form.setSlsjInfo(sjrq+"办结时间："+bjrq);
				 } else{
					 form.setSlsjInfo(sjrq);
				 }
				if(form.getZjhpwmc()!=null){
					form.setOtherInfo(form.getOtherInfo()+"。办事结果:"+form.getZjhpwmc());
				}
				String name=form.getSphjdmmc();
				//取补交告知的人员
				if("补交告知".equals(name)){
					form.setSprxm(form.getBjgzfcr());
					form.setSpyj(form.getBjgzyy()+"。补交材料清单："+form.getBjclgzqd());
				}
				if("补交受理".equals(name)){
					form.setSprxm(form.getSldwcbrxm());
					form.setSpyj("补交受理清单："+form.getBjclqd());
				}
				if("延期开始".equals(name)){
					form.setSprxm(form.getTbcxpzr());
					form.setSpyj(form.getTbcxqdlyhyj());
				}
				if("办事结果".equals(name)){
					form.setSprxm(form.getBjgzfcr());
					form.setSpyj("发放类型："+form.getFfjglx()+form.getZjhpwmc()+"。文号："+form.getZjhpwbh());
				}
				if("延期结束".equals(name)){
					form.setSpyj("结束时间:"+DateUtilsEX.dateFormate(form.getYqEndDate(),"yyyy-MM-dd"));
				}
				form.setSpyj(form.getSpyj()==null?"":form.getSpyj());
				result.add(form);
			}
			return result;
		}
		return null;
	}
	
	/**
	 * 根据受理号和环节名称查询实体
	 * @param acceptNo
	 * @param hjmc 环节名称
	 * @return
	 */
	private YwZhengwuInfo getInfoByNo(String acceptNo,String hjmc){
		String hql=" from YwZhengwuInfo where serialNo='"+acceptNo+"' and sphjdmmc='"+hjmc+"'";
		List<YwZhengwuInfo> list=this.ywZhengwuInfoDao.find(hql);
		if(list.size()>0){
			return list.get(0);
		}
		return null;
	}

	@Override
	public Page<YwZhengwuInfoForm> search(Page<YwZhengwuInfoForm> page,
			List<PropertyFilter> filters) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Page<YwZhengwuInfoForm> search(Page<YwZhengwuInfoForm> page,
			YwZhengwuInfoForm form) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void delete(Long... ids) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public YwZhengwuInfoForm get(Long id) {
		YwZhengwuInfo entity = ywZhengwuInfoDao.get(id);
		YwZhengwuInfoForm form=YwZhengwuInfoConvert.convertVoToForm(entity);
		return form;
	}

	@Override
	public void save(YwZhengwuInfoForm... forms) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void save(YwZhengwuInfoForm forms) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public List<BasicCombo> getXmmc() {
		String sql="select distinct t.xmmc from Yw_Zhengwu_Info t where t.xmmc is not null";
		SQLQuery query=this.ywZhengwuInfoDao.getSession().createSQLQuery(sql);
		List<String> list = query.list();
		List<BasicCombo> comboList = new ArrayList<BasicCombo>();
		if(list.size()>0){
			for(int i=0;i<list.size();i++){
				BasicCombo b = new BasicCombo();
				b.setLabel(list.get(i));
				b.setValue(list.get(i));
				comboList.add(b);
			}			
		}
		return comboList;
	}
	
	/**
	 * 获取环节名称
	 */
	public List<BasicCombo> getSphjdmmc(){
		String sql="select distinct t.sphjdmmc from Yw_Zhengwu_Info t where t.sphjdmmc is not null";
		SQLQuery query=this.ywZhengwuInfoDao.getSession().createSQLQuery(sql);
		List<String> list = query.list();
		List<BasicCombo> comboList = new ArrayList<BasicCombo>();
		if(list.size()>0){
			for(int i=0;i<list.size();i++){
				BasicCombo b = new BasicCombo();
				b.setLabel(list.get(i));
				b.setValue(list.get(i));
				comboList.add(b);
			}			
		}
		return comboList;
	}
}
